/***********************************************************
*discription    tablewidget控件示例
*author         david
*date           2021-5-11
*modify         2021-5-11 8：30
************************************************************/
#include "mainWindow.h"

mainWindow::mainWindow()
{
	//QRect screenRect = QApplication::desktop()->screenGeometry();
	//this.setGeometry(100, 100, 800, 400);

    //按钮控件
    pushBtn1 = new QPushButton("增加行");
    pushBtn1->setProperty("btnID", 1);
    pushBtn2 = new QPushButton("增加列");
    pushBtn2->setProperty("btnID", 2);
    pushBtn3 = new QPushButton("删除行");
    pushBtn3->setProperty("btnID", 3);
    pushBtn4 = new QPushButton("删除列");
    pushBtn4->setProperty("btnID", 4);
    pushBtn5 = new QPushButton("合并单元格");
    pushBtn5->setProperty("btnID", 5);
    tabWidget = new QTableWidget(3,3);

	/**************************表格设置************************************/
	//设置表头
	QStringList head;
	head<<"姓名"<<"年龄"<<"性别";
	tabWidget->setHorizontalHeaderLabels(head);
    //隐藏表头
    tabWidget->horizontalHeader()->setVisible(true);
    //tabWidget->horizontalHeader()->hide();
    //tabWidget->horizontalHeader()->setHidden(true);
    tabWidget->verticalHeader()->setVisible(false);
    
    //参数一表示列的 下标，参数二表示 设置指定列的宽度（单位应该是像素）。
    tabWidget->setColumnWidth(3, 140);
    tabWidget->setRowHeight(0,35);

    //列表头字体默认水平方向居中，只能设置：AlignLeft、AlignRight、AlignHCenter等属性。
    tabWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);
    tabWidget->verticalHeader()->setDefaultAlignment(Qt::AlignVCenter);
	//清空表格内容
    tabWidget->clearContents();

    //设置单元格
    tabWidget->setItem(0,0,new QTableWidgetItem("david"));
    tabWidget->setItem(0,1,new QTableWidgetItem("33"));
    tabWidget->setItem(0,2,new QTableWidgetItem("man"));
	tabWidget->setItem(1,0,new QTableWidgetItem("wenqing"));
    tabWidget->setItem(1,1,new QTableWidgetItem("38"));
    tabWidget->setItem(1,2,new QTableWidgetItem("woman"));

    //设置字体颜色
    QTableWidgetItem *item2 = new QTableWidgetItem("123");
    item2->setForeground(QBrush(QColor(255, 0, 0)));
    tabWidget->setItem(2,1,item2);
 
 	/**************************布局************************************/
	//布局
	QVBoxLayout *vLay = new QVBoxLayout();
	//QHBoxLayout *hLay = new QHBoxLayout();
	
	vLay->addWidget(pushBtn1);
	vLay->addWidget(pushBtn2);
	vLay->addWidget(pushBtn3);
	vLay->addWidget(pushBtn4);
	vLay->addWidget(pushBtn5);
	vLay->addWidget(tabWidget);
	
	//核心控件
    QWidget *pWidget = new QWidget(this);
    pWidget->setLayout(vLay);
    setCentralWidget(pWidget);
    
    //重置屏幕
    resize(600, 500);
	
	/**************************信号和槽************************************/
    connect(pushBtn1, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn2, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn3, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn4, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn5, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));

}
void mainWindow::pushBtnSlot()
{
	int num;
	QPushButton *btn = (QPushButton *)sender();
    int id = btn->property("btnID").toInt();
    switch(id)
    {
    	case 1:
    	//插入行
    	num = tabWidget->rowCount();
		tabWidget->insertRow(num);
		qDebug()<<"btn hehe"<<endl;
    	break;
    	case 2:
    	//插入列
    	num = tabWidget->columnCount();
		tabWidget->insertColumn(num);
    	break;
    	case 3:
    	//删除行
    	num = tabWidget->rowCount();
		tabWidget->removeRow(num-1);
    	break;
    	case 4:
    	//删除列
    	num = tabWidget->columnCount();
		tabWidget->removeColumn(num-1);
    	break;
    	case 5:
		//合并单元格，其实坐标，结束坐标
    	tabWidget->setSpan(2,0,2,2);
    	break;
    	default:
    	break;
    }
}

